#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDateTime>
#include <QTimer>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 创建对象
    this->labelEventFilter = new MainWindowLabelEventFilter(); // 创建事件过滤器对象

    this->pageHome = new HomeWindow(this);                     // 创建主控页面对象
    this->pageHome->move(100, 0);
    this->pageWeather = new WeatherWindow(this);               // 创建天气页面对象
    this->pageWeather->move(100, 0);
    this->pageMap = new MapWindow(this);                       // 创建地图页面对象
    this->pageMap->move(100, 0);
    this->pageVideo = new VideoWindow(this);                   // 创建视频页面对象
    this->pageVideo->move(100, 0);
    this->pageMusic = new MusicWindow(this);                   // 创建音乐页面对象
    this->pageMusic->move(100, 0);

    hidePages(); // 隐藏所有页面
    this->pageHome->show(); // 默认显示主控页面
    updateIcon(":/resources/apps/home.png");

    // 绑定信号槽
    connect(this->labelEventFilter, &MainWindowLabelEventFilter::labelClicked,
            this, &MainWindow::turnPage); // 点击图标发送，页面跳转槽函数接收

    connect(this->pageHome, &HomeWindow::musicSignal, this, &MainWindow::toMusicPage);
    connect(this->pageHome, &HomeWindow::weatherSignal, this, &MainWindow::toWeatherPage);
    connect(this->pageWeather, &WeatherWindow::backSignal, this, &MainWindow::toHomePage);
    connect(this->pageMap, &MapWindow::backSignal, this, &MainWindow::toHomePage);
    connect(this->pageVideo, &VideoWindow::backSignal, this, &MainWindow::toHomePage);
    connect(this->pageMusic, &MusicWindow::backSignal, this, &MainWindow::toHomePage);

    // 跨页面音乐播放控制
    connect(this->pageHome, &HomeWindow::musicPrevSignal, this->pageMusic, &MusicWindow::on_button_prev_clicked);
    connect(this->pageHome, &HomeWindow::musicNextSignal, this->pageMusic, &MusicWindow::on_button_next_clicked);
    connect(this->pageHome, &HomeWindow::musicToggleSignal, this->pageMusic, &MusicWindow::on_button_playState_clicked);
    connect(this->pageMusic, &MusicWindow::playStateUpdateSignal, this->pageHome, &HomeWindow::updatePlayState);
    connect(this->pageMusic, &MusicWindow::playInfoUpdateSignal, this->pageHome, &HomeWindow::updatePlayInfo);

    // 安装事件过滤器
    ui->label_home_icon->installEventFilter(labelEventFilter);    // 主控
    ui->label_weather_icon->installEventFilter(labelEventFilter); // 天气
    ui->label_map_icon->installEventFilter(labelEventFilter);     // 地图
    ui->label_video_icon->installEventFilter(labelEventFilter);   // 视频
    ui->label_music_icon->installEventFilter(labelEventFilter);   // 音乐
}

MainWindow::~MainWindow()
{
    delete ui;
}

// 隐藏页面函数
void MainWindow::hidePages()
{
    this->pageHome->hide();
    this->pageWeather->hide();
    this->pageMap->hide();
    this->pageVideo->hide();
    this->pageMusic->hide();
}

// 更新图标函数
void MainWindow::updateIcon(QString iconPath)
{
    this->setWindowIcon(QIcon(iconPath));
}

// 返回主控页面槽函数
void MainWindow::toHomePage()
{
    hidePages(); // 隐藏所有页面

    this->pageHome->show();
    updateIcon(":/resources/apps/home.png");
}

// 跳转天气页面槽函数
void MainWindow::toWeatherPage()
{
    hidePages(); // 隐藏所有页面

    this->pageWeather->show();
    updateIcon(":/resources/apps/weather.png");
}

// 跳转音乐页面槽函数
void MainWindow::toMusicPage()
{
    hidePages(); // 隐藏所有页面

    this->pageMusic->show();
    updateIcon(":/resources/apps/music.png");
}

// 页面跳转槽函数
void MainWindow::turnPage(QObject *watched)
{
    QString objName = watched->objectName(); // 获取被点击图标的名称

    hidePages(); // 隐藏所有页面

    if (objName == "label_home_icon") // 跳转主控页面
    {
        this->pageHome->show();
        updateIcon(":/resources/apps/home.png");
    }
    else if (objName == "label_weather_icon") // 跳转天气页面
    {
        this->pageWeather->show();
        updateIcon(":/resources/apps/weather.png");
    }
    else if (objName == "label_map_icon") // 跳转地图页面
    {
        this->pageMap->show();
        updateIcon(":/resources/apps/map.png");
    }
    else if (objName == "label_video_icon") // 跳转视频页面
    {
        this->pageVideo->show();
        updateIcon(":/resources/apps/video.png");
    }
    else if (objName == "label_music_icon") // 跳转音乐页面
    {
        this->pageMusic->show();
        updateIcon(":/resources/apps/music.png");
    }
}

